# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.shortcuts import render
from django.http import JsonResponse
#from django.views.decorators.csrf import csrf_exempt,csrf_protect #
from .models import FormtableMain59Dt1,FormtableMain59,UfZckp 
from django.db import connection,transaction

# Create your views here.
def login(request):
    if request.method == 'POST':
        search_value = request.POST.get('search_value')
        type = request.POST.get('type')
        cursor = connection.cursor()
        if type == 'C1':
            cursor.execute('SELECT T1.LCBH,T2.BM,T2.ZCMC,T2.GGXH FROM FORMTABLE_MAIN_59 T1 JOIN FORMTABLE_MAIN_59_DT1 T2 ON T1.ID = T2.MAINID WHERE T1.LCBH = %s',[search_value])
            result = cursor.fetchall()
            list_bm = []
            list_zcmc = []
            list_ggxh = []
            for i in range(len(result)):
                list_bm.append(result[i][1])
                list_zcmc.append(result[i][2])
                list_ggxh.append(result[i][3])
            return JsonResponse({'bm':list_bm,'zcmc':list_zcmc,'ggxh':list_ggxh})
        elif type == 'C2':
            
            cursor.execute('SELECT BM,ZCMC,GGXH from UF_ZCKP WHERE BM = %s',[search_value])
            #cursor.execute('SELECT BM,ZCMC,GGXH from UF_ZCKP WHERE BM = %s',['DS8041703090394'])
            result = cursor.fetchall()
            list_bm = []
            list_zcmc = []
            list_ggxh = []
            for i in range(len(result)):
                list_bm.append(result[i][0])
                list_zcmc.append(result[i][1]) 
                list_ggxh.append(result[i][2])
            return JsonResponse({'bm':search_value,'zcmc':list_zcmc,'ggxh':list_ggxh})
    else:
        return render(request,'user/login.html') 